package com.cpl.web.validator;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;

import com.cpl.common.pojo.Business;
import com.cpl.common.pojo.Customer;

@Component("userLoginValidator")
public class UserLoginValidator {
	public void validate(final Object obj, final Errors err) {
		if (obj instanceof Customer) {
			validateCustomer((Customer) obj, err);
		} else if (obj instanceof Business) {
			validateBusiness((Business) obj, err);
		}
	}

	private void validateBusiness(Business business, Errors err) {
		String userName = business.getUserName();
		String password = business.getPassword();
		if (StringUtils.isEmpty(userName)) {
			err.rejectValue("userName", "userName.empty",
					"User Name cant be empty");
		}
		if (StringUtils.isEmpty(password)) {
			err.rejectValue("password", "password.empty",
					"Password Cant be empty");
		}

	}

	private void validateCustomer(Customer customer, Errors err) {
		String userName = customer.getUserName();
		String password = customer.getPassword();
		if (StringUtils.isEmpty(userName)) {
			err.rejectValue("userName", "userName.empty",
					"User Name cant be empty");
		}
		if (StringUtils.isEmpty(password)) {
			err.rejectValue("password", "password.empty",
					"Password Cant be empty");
		}

	}

}
